home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Security
/
Hotspot Shield 1.08
/
HSS-1.08-install-anchorfree-76-conduit.exe
/
components
/
Conduit.js
next >
Wrap
Text File
|
2008-06-26
|
13KB
|
404 lines
function Fix404Obj(strUrl, strCTID)
{
this.url = strUrl;
this.CTID = strCTID;
}
//helper functions
function GetMostRecentBrowserWindow()
{
var objWindowsManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
.getService(Components.interfaces.nsIWindowMediator);
var objRecentBrowserWindow = objWindowsManager.getMostRecentWindow('navigator:browser');
return objRecentBrowserWindow;
}
var EBServerDataURL =
{
ServerRequest : function(strURL,strPostData,strUserName,strPassword,ServerResponseFunction)
{
var objIOService = Components.classes["@mozilla.org/network/io-service;1"].createInstance(Components.interfaces.nsIIOService);
var objURI = objIOService.newURI(strURL, null, null);
if(strUserName != null && strPassword != null)
{
objURI.username = strUserName;
objURI.password = strPassword;
}
var objChannel = objIOService.newChannelFromURI(objURI);
objChannel.QueryInterface(Components.interfaces.nsIHttpChannel).setRequestHeader('PRAGMA','NO-CACHE',false);
objChannel.QueryInterface(Components.interfaces.nsIHttpChannel).setRequestHeader('CACHE-CONTROL','NO-CACHE',false);
if(strPostData != null)
{
var objUploadStream = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream);
objUploadStream.setData(strPostData, strPostData.length);
var objUploadChannel = objChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
objUploadChannel.setUploadStream(objUploadStream, "application/x-www-form-urlencoded", -1);
objChannel.QueryInterface(Components.interfaces.nsIHttpChannel).requestMethod = "POST";
}
var objObserver = new this.Observer(ServerResponseFunction);
objChannel.asyncOpen(objObserver, null);
},
Observer: function(ServerResponseFunction)
{
return ({
Data : "",
onStartRequest: function(aRequest, aContext)
{
this.Data = "";
},
onStopRequest: function(aRequest, aContext, aStatus)
{
if(ServerResponseFunction)
{
ServerResponseFunction(this.Data, aRequest);
}
},
onDataAvailable: function(aRequest, aContext, aStream, aSourceOffset, aLength)
{
var objScriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
objScriptableInputStream.init(aStream);
this.Data += objScriptableInputStream.read(aLength);
}
});
}
}
//Browser Progress Listener
var XPCOMEBBrowserProgressListener =
{
QueryInterface: function(aIID)
{
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onStateChange: function(aProgress, aRequest, aFlag, aStatus)
{
var strURL = '';
if(aRequest)
strURL = aRequest.name;
if(aFlag & Components.interfaces.nsIWebProgressListener.STATE_STOP)
{
var bIsSuccess = Components.isSuccessCode(aStatus);
var responseStatus = 200;
var iContentLength = 0;
try
{
httpChannel = aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
responseStatus = httpChannel.responseStatus;
iContentLength = aRequest.QueryInterface(Components.interfaces.nsIChannel).contentLength;
}
catch(ex){}
var oWindow = aProgress.DOMWindow;
var objConduit = Components.classes["@conduit.com/helper;3"]
.getService(Components.interfaces.nsIConduit);
var strRedirectUrl = objConduit.retrieve404redirectUrl();
var strPreservedUrl = oWindow.document.baseURI;
//executes when 404 custom page is received
var oCallback = function(strResponse)
{
//check if the page is legal
if(strResponse.substring(0,35).indexOf("<!--FIX_PAGE_NOT_FOUND_REMARK-->") == -1)
return;
try
{
strResponse = strResponse.replace(/<!--FIX_PAGE_NOT_FOUND_REMARK-->/,'');
strResponse = strResponse.replace(/<!--/g,'');
strResponse = strResponse.replace(/-->/g,'');
strResponse = strResponse.replace(/\'/g,"\\'");
strResponse = strResponse.replace(/\r\n/g,"");
strResponse = strResponse.replace(/\/\/\<\!\[CDATA\[/g,'');
strResponse = strResponse.replace(/\/\/]]>/g,'');
var strJSInnerHTML = "document.write('"+strResponse+"')";
var oScript = oWindow.document.createElement('script');
oScript.setAttribute('type','text/javascript');
oScript.innerHTML = strJSInnerHTML;
oWindow.document.getElementsByTagName('head')[0].appendChild(oScript);
}
catch(e) {}
};
//handle 404 error
//if(responseStatus == 404 || !Components.isSuccessCode(aStatus))
if(responseStatus == 404 && iContentLength <= 1635)
{
if(strRedirectUrl)
{
try
{
aRequest.cancel(aRequest.NS_ERROR_FAILURE);
strRedirectUrl = strRedirectUrl.replace(/EB_MAIN_FRAME_URL/g, strPreservedUrl);
EBServerDataURL.ServerRequest(strRedirectUrl, null, null, null, oCallback);
}
catch(ex) {}
}
}
}
return 0;
},
onLocationChange: function(aProgress, aRequest, aURI) {return 0;},
onProgressChange: function() {return 0;},
onStatusChange: function() {return 0;},
onSecurityChange: function(aProgress, aRequest, aState) {return 0;},
onLinkIconAvailable: function() {return 0;}
}
//XPCOM
function ConduitHelperObject() {
this.Data = new Array();
this.RedirectArr = new Array();
this.isListenerAttached = false;
}
ConduitHelperObject.prototype = {
//Global key - value storage
storeKey : function(strCTID, strKey, strValue)
{
if(!this.Data[strCTID])
this.Data[strCTID] = new Array();
this.Data[strCTID][strKey] = strValue;
},
retrieveKey : function(strCTID, strKey)
{
return this.Data[strCTID][strKey];
},
deleteKey : function(strCTID, strKey)
{
delete this.Data[strCTID][strKey];
},
// Fix not found page functions
registerSingleWindow404redirectUrl : function(strUrl, strCTID)
{
this._addUrlToList(strUrl, strCTID);
this._addProgressListener(false);
},
unregisterSingleWindow404redirectUrl : function(strUrl, strCTID)
{
this._removeUrlFromList(strUrl, strCTID);
if(this.RedirectArr.length == 0)
this._removeProgressListener(false);
},
registerAllWindows404redirectUrl : function(strUrl, strCTID)
{
this._addUrlToList(strUrl, strCTID);
this._addProgressListener(true);
},
unregisterAllWindows404redirectUrl : function(strUrl, strCTID)
{
this._removeUrlFromList(strUrl, strCTID);
if(this.RedirectArr.length == 0)
this._removeProgressListener(true);
},
retrieve404redirectUrl : function()
{
if(this.RedirectArr.length == 0)
return "";
else
return this.RedirectArr[this.RedirectArr.length-1].url;
},
_addUrlToList : function(strUrl, strCTID)
{
if(this.RedirectArr.length == 0)
this.RedirectArr.push(new Fix404Obj(strUrl, strCTID));
else
{
var bIsFound = false;
for(var i=0; i<this.RedirectArr.length; i++)
if(this.RedirectArr[i].CTID == strCTID)
{
bIsFound = true;
//update the url
this.RedirectArr[i].url = strUrl;
break;
}
if(!bIsFound)
this.RedirectArr.push(new Fix404Obj(strUrl, strCTID));
}
},
_removeUrlFromList : function(strUrl, strCTID)
{
if(this.RedirectArr.length == 0)
return;
var iIndexToRemove = -1;
for(var i=0; i<this.RedirectArr.length; i++)
if(this.RedirectArr[i].CTID == strCTID)
{
iIndexToRemove = i;
break;
}
if(iIndexToRemove != -1)
this.RedirectArr.splice(iIndexToRemove, 1);
},
_addProgressListener : function(bIsAll)
{
if(bIsAll)
{
var objWindowsManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
.getService(Components.interfaces.nsIWindowMediator);
var objEnumerator = objWindowsManager.getEnumerator('navigator:browser');
while(objEnumerator.hasMoreElements())
{
var oWindow = objEnumerator.getNext();
if(oWindow.getBrowser)
{
try
{
if(!oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
{
oWindow.getBrowser().setAttribute('EBMainListenerSet',true);
oWindow.getBrowser().addProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
}
}
catch(ex){}
}
}
}
else
{
var oWindow = GetMostRecentBrowserWindow();
if(!oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
{
oWindow.getBrowser().setAttribute('EBMainListenerSet',true);
oWindow.getBrowser().addProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
}
}
},
_removeProgressListener : function(bIsAll)
{
if(bIsAll)
{
var objWindowsManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
.getService(Components.interfaces.nsIWindowMediator);
var objEnumerator = objWindowsManager.getEnumerator('navigator:browser');
while(objEnumerator.hasMoreElements())
{
var oWindow = objEnumerator.getNext();
if(oWindow.getBrowser)
{
try
{
if(oWindow && oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
{
oWindow.getBrowser().removeAttribute('EBMainListenerSet');
oWindow.getBrowser().removeProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
}
}
catch(ex){}
}
}
}
else
{
var oWindow = GetMostRecentBrowserWindow();
if(oWindow && oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
{
oWindow.getBrowser().removeAttribute('EBMainListenerSet');
oWindow.getBrowser().removeProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
}
}
},
QueryInterface: function (uuid) {
if (uuid.equals(Components.interfaces.nsIConduit) ||
uuid.equals(Components.interfaces.nsISupports)) {
return this;
}
Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
return null;
},
}
const COMPONENT_ID = Components.ID("{B009C480-CA54-4483-BA59-5505DD98BE40}");
var ConduitHelperObjectModule = {
registerSelf: function (compMgr, fileSpec, location, type) {
compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
compMgr.registerFactoryLocation(COMPONENT_ID,
"Conduit Helper Object",
"@conduit.com/helper;3",
fileSpec,
location,
type);
},
getClassObject: function (compMgr, cid, iid) {
if (!cid.equals(COMPONENT_ID))
throw Components.results.NS_ERROR_NO_INTERFACE;
if (!iid.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
return ConduitHelperObjectFactory;
},
canUnload: function(compMgr) {
return true;
}
};
var ConduitHelperObjectFactory = {
createInstance: function (outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
return new ConduitHelperObject().QueryInterface(iid);
}
};
function NSGetModule(compMgr, fileSpec) {
return ConduitHelperObjectModule;
}